<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/views/common/include/taglib.jsp"%>
	<link rel="stylesheet" href="${ctx}/js/lib/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
	<link type="text/css" rel="stylesheet" href="${ctx}/css/dlifestyle/org.css" />
	<script type="text/javascript" src="${ctx}/js/lib/ztree/jquery.ztree.all-3.5.js"></script>
	<script type="text/javascript">
	function setCtx(ctx){
		this.ctx = ctx;
	}
	</script>
	
			<section class="scrollable padder">
              <div style="height:40px;line-height: 40px;">
                    <div class="forword_area"></div>
					<div class="location">当前位置：${sessionScope.currcent_position}</div>

              </div>
              <hr>
		              <section>
		              	
					    <!--begin 搜索栏 -->
			                  <div class="panel" >
				                  <div style="float:left;height: 28px;">
				                  
							        <!--搜索框-->
						        	<!-- <label>部门名称：</label> -->
						            <input id="search_condition" type="text" placeholder="部门名称" class="input-sm" />
								    </div>
						             <div class="form-control">
						            	 <div class="search" onclick="search_ztree('treeDemo', 'search_condition')"><img src="${ctx }/images/table/search_second.png"></div>
						            	 <div class="search" onclick="addOrgClick();"><img src="${ctx }/images/table/add_second.png"></div>
						            	 <div class="search" onclick="edit();"><img src="${ctx }/images/table/edi_second.png"></div>
						             	 <input type="hidden" id="callbackTrigger" />
									     <div class="delete" id="remove" title="删除" onclick="return false;">
											<img src="${ctx }/images/table/del_second.png">
										</div>
						             </div>
					           </div>
			                <!--end 搜索栏 -->
		              	
		              		<!--生成树  -->
							<div class="content_wrap" style="width:210px;float:left;margin-top: 20px;">
								<div class="modal-header">
									<ul style="min-height: 40px;line-height: 40px;margin-left: 20px;">
									<li style="display:inline-block;list-style:none;">
										<img src="${ctx }/images/system/org_title.png">
									</li>
									<li style="display:inline-block;width: 100px;">
										<h4 class="modal-title" style="text-indent: 0;">部门</h4>
									</li>
									</ul>
								</div>
								<div class="zTreeDemoBackground left">
									<ul id="treeDemo" class="ztree"></ul>
								</div>
							</div>
							
							<!--部门详情  -->
							<div id="orgDetail" class="right" style=" margin-left: 280px;  margin-top: 20px;">
									<div class="modal-header">
										<h4 class="modal-title">部门详细信息</h4>
									</div>
								
									<!-- begin table表格 -->
									<table  style="border:1px solid #B4B4B4;  border-collapse: collapse;width:100%;">
										<tr>
											<td width="20%" class="word">部门名称： </td>
											<td style="text-align:left;text-indent: 2em;color:#565252;font-size: 13px;"><span id="orgName"></span></td>
										</tr>
										<tr>
											<td width="20%" class="word" style="background-color: #FDFDFD;">部门描述：</td>
											<td style="text-align:left;text-indent: 2em;background-color: #FDFDFD;color:#565252;font-size: 13px;"><span id="descrip"></span></td>
										</tr>
										<tr>
											<td width="20%" class="word">部门电话：</td>
											<td style="text-align:left;text-indent: 2em;color:#565252;font-size: 13px;"><span id="connectPhone"></span></td>
										</tr>
										<tr>
											<td width="20%" class="word" style="background-color: #FDFDFD;">办公地点：</td>
											<td style="text-align:left;text-indent: 2em;background-color: #FDFDFD;color:#565252;font-size: 13px;"><span id="location"></span></td>
										</tr>
									</table>
								
							</div>
								
							<!--编辑部门  -->
							<div id="editOrg" style="display:none;margin-left: 280px;  margin-top: 20px;" >
								<div class="modal-header" style="height:40px;">
							        <h4 class="modal-title ng-binding" style="float:left;">编辑部门</h4>
							    </div>
								<form class="myForm">
									<div class="modal-body">
										<div style=" width:100%;height:50px;  line-height: 50px;">
											<label class="col-sm-3 editlable">部门名称&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
											<div class="col-sm-9 editdiv">
												<input type="hidden" id="nodeId">
												<input id="name" class="editinput" onblur="checkOrgName()" type="text" maxlength="10" placeholder="部门名称" required>
											</div>
										</div>
										<div  style="width:100%;height:110px;  line-height: 110px;">
											<label class="col-sm-3 editlable">部门描述&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
											<div class="col-sm-9 editdiv" style="height: 110px;">
												<textarea rows="5" class="editinput" onblur="checkDescrip()" onpropertychange="if(value.length>200) value=value.substr(0,200)" maxlength="200"  id="description"
			                        			class="form-control" placeholder="部门描述" style="resize:none;" required></textarea>
											</div>
										</div>
											<div style="width:100%;height:50px;  line-height: 50px;">
												<label class="col-sm-3 editlable">部门电话&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
												<div class="col-sm-9 editdiv">
													<input id="tel" type="text" class="editinput" onblur="checkTel()" maxlength="13"
														class="form-control tel" placeholder="部门电话" required>
												</div>
											</div>
											<div  style="width:100%;height:50px;  line-height: 50px;">
												<label class="col-sm-3 editlable">办公地点&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
												<div class="col-sm-9 editdiv">
													<input  onblur="checkLocation()" class="editinput" type="text"  id="locate"
														maxlength="35" class="form-control" placeholder="办公地点" required maxlength="50">
												</div>
											</div>
											</div>
								<footer class="panel-footer">
									<div>
										 <button class ="btn-success" type="button" onclick="updateOrg()" class="popupbutton ng-binding">确定</button>&nbsp;
			      						 <button class ="btn-success" type="button" class="popupbutton ng-binding" onclick="hideEditOrg()">取消</button>
									</div>
								</footer>
								</form>
							</div>
								
								<!--新增部门  -->
								<div id="addOrg" style="display:none;margin-left: 280px;  margin-top: 20px;">
									<div class="modal-header" style="height:40px;">
								        <h4 id="addOrgTitle" class="modal-title ng-binding" style="float:left;">新增部门</h4>
								    </div>
									<form class="myForm">
										<div class="modal-body">
											<div class="form-group" style="width:100%;height:50px;  line-height: 50px;">
												<label class="col-sm-3 editlable">部门名称&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
												<div class="col-sm-9 editdiv">
													<input id="aname" class="editinput" onblur="acheckOrgName()" type="text" maxlength="10" placeholder="部门名称" required>
												</div>
											</div>
											<div class="form-group" style="width:100%;height:110px;  line-height: 110px;">
												<label class="col-sm-3 editlable">部门描述&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
												<div class="col-sm-9 editdiv" style="height: 110px;">
													<textarea rows="5" class="editinput" onblur="acheckDescrip()" onpropertychange="if(value.length>200) value=value.substr(0,200)" maxlength="200"  id="adescription"
				                        			class="form-control" placeholder="部门描述" style="resize:none;" required></textarea>
												</div>
											</div>
												<div class="form-group" style="width:100%;height:50px;  line-height: 50px;">
													<label class="col-sm-3 editlable">部门电话&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
													<div class="col-sm-9 editdiv">
														<input id="atel" type="text" class="editinput" onblur="acheckTel()" maxlength="13"
															class="form-control tel" placeholder="部门电话" required>
													</div>
												</div>
												<div class="form-group" style="width:100%;height:50px;  line-height: 50px;">
													<label class="col-sm-3 editlable">办公地点&nbsp;<em style="color: red;font-size: 15px;">*</em></label>
													<div class="col-sm-9 editdiv">
														<input  onblur="acheckLocation()" class="editinput" type="text"  id="alocate"
															maxlength="35" class="form-control" placeholder="办公地点" required maxlength="50">
													</div>
												</div>
												</div>
									<footer class="panel-footer">
										<div>
											 <button class ="btn-success" type="button" onclick="addOrg()" class="popupbutton ng-binding">确定</button>&nbsp;
				      						 <button class ="btn-success" type="button" class="popupbutton ng-binding" onclick="hideAddOrg()">取消</button>
										</div>
									</footer>
									</form>
								</div>
		            </section>
			</section>
		<input type="hidden" id="orgPid"/>
	<%@include file="/views/common/footer.jsp"%>
  	<script type="text/javascript">
		var ctx = '${ctx}';
		
		/* document.getElementById("myFrom").reset(); */
		

		var curMenu = null, zTree_Menu = null;
		var setting = {
			view: {
				showLine: true,
				showIcon: true,
				selectedMulti: false,
				dblClickExpand: false,
				addDiyDom: addDiyDom,
				fontCss: setFontCss_ztree,
			},
			edit: {
				enable: true,
				showRemoveBtn: false,
				showRenameBtn: false
				},
			data: {
				keep: {
					parent:true,
					leaf:true
				},
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pId",
					rootPid: null 
				},
			},
			callback: {
				/* beforeClick: beforeClick, */
				onClick : onClick,
				onAsyncSuccess: zTreeOnAsyncSuccess,
				beforeDrag: beforeDrag
			}
		};

  		var zNodes;
  		
  		function onClick(event, treeId, treeNode, clickFlag) {  
  			
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			
			loading(treeNode);

  		}
  		
  		function loading(treeNode){
 		   $.ajax({
			    async:true,//是否异步
			    cache:false,//是否使用缓存
			    type:'get',//请求方式：post
			    dataType:'json',//数据传输格式：json
			    url:ctx + "/org/orgDetail?id=" + treeNode.id ,//请求的action路径
			    error:function(){
			      //请求失败处理函数
			    	layer.msg('请求失败!', {icon: 5});
			    },
			    success:function(data){
			    	$("#orgName").text(data.orgName);
			    	$("#descrip").text(data.descrip);
			    	$("#connectPhone").text(data.connectPhone);
			    	$("#location").text(data.location);
   		   }
  			});
  		}
  		
 		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
 			zNodes = JSON.parse(msg);
 			layer.msg(msg, {icon: 5});
 		};
 			
		function addDiyDom(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch"),
			icoObj = $("#" + treeNode.tId + "_ico");
			switchObj.remove();
			icoObj.before(switchObj);

			if (treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
				switchObj.before(spaceStr);
			}
		}

		
		var treeNodes;
		 
		function onLoadZTree(){
			 
			   $.ajax({
			    async:true,//是否异步
			    cache:false,//是否使用缓存
			    type:'post',//请求方式：post
			    dataType:'json',//数据传输格式：json
			    url:ctx + "/org/treeData",//请求的action路径
			    error:function(){
			      //请求失败处理函数
			      layer.msg('请求失败!', {icon: 5});
			    },
			    success:function(data){
			    	if(!data.success){
			    		layer.msg(data.msg, {icon: 5});
			    		return;
			    	}

			      	var t = $("#treeDemo");
					 $.fn.zTree.init(t, setting, eval(data.obj));
					 
					 //初始化树时默认选择第一个节点
						zTree_Menu = $.fn.zTree.getZTreeObj("treeDemo");
						curMenu = zTree_Menu.getNodes();
						
					      if (curMenu.length>0) 
					      {
					            zTree_Menu.selectNode(curMenu[0]);
					            
					            loading(curMenu[0]);
					            
					      }

						t.hover(function () {
							if (!t.hasClass("showIcon")) {
								t.addClass("showIcon");
							}
						}, function() {
							t.removeClass("showIcon");
						});
						
						$("#edit").bind("click", edit);
						$("#remove").bind("click", remove);
			    }
			  });
			  
			  
			}
		
		$(document).ready(function(){
			
			onLoadZTree();
			
			/* $("#orgLevel").hide(); */
		});
			
		
		//==========================搜索树begin===================================//
		
		/**
     * 展开树
     * @param treeId  
     */
    function expand_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
    }
     
    /**
     * 收起树：只展开根节点下的一级节点
     * @param treeId
     */
    function close_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var nodes = treeObj.transformToArray(treeObj.getNodes());
        var nodeLength = nodes.length;
        for (var i = 0; i < nodeLength; i++) {
            if (nodes[i].id == '0') {
                //根节点：展开
                treeObj.expandNode(nodes[i], true, true, false);
            } else {
                //非根节点：收起
                treeObj.expandNode(nodes[i], false, true, false);
            }
        }
    }
     
    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId 文本框的id
     */
    function search_ztree(treeId, searchConditionId){
        searchByFlag_ztree(treeId, searchConditionId, "");
    }
     
    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId     搜索条件Id
     * @param flag                  需要高亮显示的节点标识
     */
    function searchByFlag_ztree(treeId, searchConditionId, flag){
        //<1>.搜索条件
        var searchCondition = $('#' + searchConditionId).val();
      	//解决ie9以下placeholder与value重复的问题
    	if(searchCondition == $('#' + searchConditionId).attr("placeholder")){
    		searchCondition = "";
    	}
        //<2>.得到模糊匹配搜索条件的节点数组集合
        var highlightNodes = new Array();
        if (searchCondition != "") {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            highlightNodes = treeObj.getNodesByParamFuzzy("name", searchCondition, null);
        }
        //<3>.高亮显示并展示【指定节点s】
        highlightAndExpand_ztree(treeId, highlightNodes, flag);
    }
     
    /**
     * 高亮显示并展示【指定节点s】
     * @param treeId
     * @param highlightNodes 需要高亮显示的节点数组
     * @param flag           需要高亮显示的节点标识
     */
    function highlightAndExpand_ztree(treeId, highlightNodes, flag){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }
        //<2>.收起树, 只展开根节点下的一级节点
        close_ztree(treeId);
        //<3>.把指定节点的样式更新为高亮显示，并展开
        if (highlightNodes != null) {
            for (var i = 0; i < highlightNodes.length; i++) {
                if (flag != null && flag != "") {
                    if (highlightNodes[i].flag == flag) {
                        //高亮显示节点，并展开
                        highlightNodes[i].highlight = true;
                        treeObj.updateNode(highlightNodes[i]);
                        //高亮显示节点的父节点的父节点....直到根节点，并展示
                        var parentNode = highlightNodes[i].getParentNode();
                        var parentNodes = getParentNodes_ztree(treeId, parentNode);
                        treeObj.expandNode(parentNodes, true, true, false);
                        treeObj.expandNode(parentNode, true, true, false);
                    }
                } else {
                    //高亮显示节点，并展开
                    highlightNodes[i].highlight = true;
                    treeObj.updateNode(highlightNodes[i]);
                    //高亮显示节点的父节点的父节点....直到根节点，并展示
                    var parentNode = highlightNodes[i].getParentNode();
                    var parentNodes = getParentNodes_ztree(treeId, parentNode);
                    treeObj.expandNode(parentNodes, true, true, false);
                    treeObj.expandNode(parentNode, true, true, false);
                }
            }
        }
    }
     
    /**
     * 递归得到指定节点的父节点的父节点....直到根节点
     */
    function getParentNodes_ztree(treeId, node){
        if (node != null) {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            var parentNode = node.getParentNode();
            return getParentNodes_ztree(treeId, parentNode);
        } else {
            return node;
        }
    }
     
    /**
     * 设置树节点字体样式
     */
     function setFontCss_ztree(treeId, treeNode) {
         /* if (treeNode.id == 0) {
             //根节点
             return {color:"#333", "font-weight":"bold"};
         } else if (treeNode.isParent == false){
             //叶子节点
             return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#660099", "font-weight":"normal"};
         } else {
             //父节点
             return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
         } */
     }
 
 
		
		//==========================搜索树end===================================//
		
		//==========================删除树begin===================================//
		
		function beforeDrag(treeId, treeNodes) {
			return false;
		}

		function remove(e) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			if (nodes.length == 0) {
				layer.msg('请先选择一个节点!', {icon: 5});
				return;
			}
			var callbackFlag = $("#callbackTrigger").attr("checked");
			/* zTree.removeNode(treeNode, callbackFlag); */
			
			var confirmVal = false; 
			//询问框
			layer.confirm('确认删除分类[ ' + treeNode.name + ' ]吗？', {
			    btn: ['确定','取消'] //按钮
			}, function(index){
				layer.close(index);
				var data = {id:treeNode.id};
				layer.load(1);
                $.ajax({  
                     async: false,  
                     type: "get",  
                     data:data,  
                     url: ctx + "/org/delOrg ",  
                     success: function(data){
                    	 layer.closeAll('loading');
                         if(data.success){  
                        	layer.msg('删除成功!');
                            setTimeout('loadContent("/org/orgList");',500);
                          } else{  
                        	layer.msg(data.msg, {icon: 5});
                          }  
                     },  
                     error: function(){  
                    	 layer.closeAll('loading');
                    	 layer.msg('删除失败!', {icon: 5});  
                     }  
                });  
			});
			
            return confirmVal; 
		};

		
		//==========================删除树end===================================//
		
		//==========================编辑树end===================================//
		
			    //===================编辑校验============================//
			//验证部门电话是否符合格式
			 function checkTel(){
				var tel = $("#tel").val();
				var checktel = document.getElementById("checktel");
				if(tel == null || tel == ""){
					layer.tips('部门电话不能为空', '#tel', {
					    tips: 3
					});
				}else{
					var reg= /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
					if(!reg.test(tel)){
						layer.tips('电话号码格式错误，可填固定电话或手机', '#tel', {
						    tips: 3
						});
						return false;
					}else{
						return true;
					}
				}
			} 
	     
			//验证部门不能为空
			 function checkOrgName(){
				 var name = $("#name").val();
					var checkname = document.getElementById("checkname");
					if(name == null || name == ""){
						layer.tips('部门不能为空', '#name', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
			//验证部门描述不能为空
			 function checkDescrip(){
				 var descrip = $("#description").val();
					var checkdescrip = document.getElementById("checkdescrip");
					if(descrip == null || descrip == ""){
						layer.tips('描述不能为空', '#description', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
			//验证部门地址不能为空
			 function checkLocation(){
				 var location = $("#locate").val();
					var checklocation = document.getElementById("checklocation");
					if(location == null || location == ""){
						layer.tips('部门地址不能为空', '#locate', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
		
		function edit() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes();
			if (nodes.length == 0) {
				layer.msg('请先选择一个节点!', {icon: 5});
				return;
			}else{
				
				$("#editOrg").show();
				$("#orgDetail").hide();
				$("#addOrg").hide();
				
				//选取当前选中节点
				treeNode = nodes[0];
				
	 		   $.ajax({
				    async:true,//是否异步
				    cache:false,//是否使用缓存
				    type:'get',//请求方式：post
				    dataType:'json',//数据传输格式：json
				    url:ctx + "/org/orgDetail?id=" + treeNode.id ,//请求的action路径
				    error:function(){
				      //请求失败处理函数
				      layer.msg('请求失败!', {icon: 5});
				    },
				    success:function(data){
				    	$("#name").val(data.orgName);
				    	$("#description").val(data.descrip);
				    	$("#tel").val(data.connectPhone);
				    	$("#locate").val(data.location);
				    	$("#nodeId").val(data.id);
	   		   }
	  			});
				
			}
		};
		
		function updateOrg(){
 			
        	var orgName = $("#name").val();
	     	var descrip = $("#description").val();
	     	var connectPhone = $("#tel").val();
	     	var location = $("#locate").val();
	     	var orgId = $("#nodeId").val();
	     		if (checkTel() && checkOrgName() && checkDescrip() && checkLocation()){
	     			layer.load(1);
	    	     	$.ajax({
	   	             type : "POST",
	   	             url :ctx + "/org/updateOrg",
	   	             cache : false,
	   	             dataType : 'json',
	   	             data:{
	   	            	orgName : orgName,
	   	            	descrip : descrip,
	   	            	connectPhone : connectPhone,
	   	            	location : location,
	   	            	id : orgId
	   	             },
	   	             success : function(result) {
	   	            	layer.closeAll('loading');
	   	             	if (result.success) {
	   	             		layer.msg('修改成功!');
	   	             	    setTimeout('loadContent("/org/orgList");',500); 
	   	             	 
   	             		} else {
   	             			layer.msg(result.msg, {icon: 5});
	   	             	}
	   	             },  
                     error: function(){  
                    	 layer.closeAll('loading');
                    	 layer.msg('修改失败!', {icon: 5});  
                     } 
	   	     	});
	     		}  
		}
		
		//取消编辑
		function hideEditOrg(){
			$("#orgDetail").show();
			$("#editOrg").hide();
		}
		//==========================编辑树end===================================//
		
		//==========================新增树end===================================//
		
					//============================保存校验================================//
			//验证部门电话是否符合格式
			 function acheckTel(){
				var tel = $("#atel").val();
				//解决ie9以下placeholder与value重复的问题
				if(tel == $("#atel").attr("placeholder")){
					tel = "";
				}
				var checktel = document.getElementById("achecktel");
				if(tel == null || tel == ""){
					layer.tips('部门电话不能为空', '#atel', {
					    tips: 3
					});
				}else{
					var reg= /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
					if(!reg.test(tel)){
						layer.tips('电话号码格式错误，可填固定电话或手机', '#atel', {
						    tips: 3
						});
						return false;
					}else{
						return true;
					}
				}
			} 
	     
			//验证部门不能为空
			 function acheckOrgName(){
				 var name = $("#aname").val();
				//解决ie9以下placeholder与value重复的问题
					if(name == $("#aname").attr("placeholder")){
						name = "";
					}
					var checkname = document.getElementById("acheckname");
					if(name == null || name == ""){
						layer.tips('部门不能为空', '#aname', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
			//验证部门描述不能为空
			 function acheckDescrip(){
				 var descrip = $("#adescription").val();
				//解决ie9以下placeholder与value重复的问题
					if(descrip == $("#adescription").attr("placeholder")){
						descrip = "";
					}
					var checkdescrip = document.getElementById("acheckdescrip");
					if(descrip == null || descrip == ""){
						layer.tips('描述不能为空', '#adescription', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
			//验证部门地址不能为空
			 function acheckLocation(){
				 var location = $("#alocate").val();
				//解决ie9以下placeholder与value重复的问题
					if(location == $("#alocate").attr("placeholder")){
						location = "";
					}
					var checklocation = document.getElementById("achecklocation");
					if(location == null || location == ""){
						layer.tips('部门地址不能为空', '#alocate', {
						    tips: 3
						});
					}else{
						return true;
				}
			}
			 
		function addOrgClick(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes();
			if (nodes.length == 0) {
				layer.msg('请先选择一个节点!', {icon: 5});
				return;
			}
			
			//询问框
			layer.confirm('请选择新增部门类型(相对于' + nodes[0].name + ')', {
			    btn: ['同级部门','下级部门','取消'] //按钮
			}, function(index){
				layer.close(index);
				setLevel(nodes[0], 2);
			}, function(){
				setLevel(nodes[0], 3);
			});
		}
		
		//选择菜单等级
		function setLevel(treeNode, level){
			var orgPid;
			
			if(level == 1){
			    orgPid = 0;
			}else if(level == 2){
				orgPid = treeNode.pId;
				$("#addOrgTitle").html("新增" + treeNode.name + "同级部门");
			}else if(level == 3){
				orgPid = treeNode.id;
				$("#addOrgTitle").html("新增" + treeNode.name + "下级部门");
			}
			
			$("#orgPid").val(orgPid);
			
			/* $("#orgLevel").hide(); */
			
			$("#addOrg").show();
			$("#editOrg").hide();
			$("#orgDetail").hide();
			$("#addOrgTitle").val("新增" + treeNode.name + "同级部门");
		}
		
		function addOrg() {

        	var orgName = $("#aname").val();
	     	var descrip = $("#adescription").val();
	     	var connectPhone = $("#atel").val();
	     	var location = $("#alocate").val();
	     	var orgPid = $("#orgPid").val();
	     		if (acheckTel() && acheckOrgName() && acheckDescrip() && acheckLocation()){
	     			layer.load(1);
	    	     	$.ajax({
	   	             type : "POST",
	   	             url :ctx + "/org/addOrg",
	   	             cache : false,
	   	             dataType : 'json',
	   	             data:{
	   	            	orgName : orgName,
	   	            	descrip : descrip,
	   	            	connectPhone : connectPhone,
	   	            	location : location,
	   	            	orgPid : orgPid
	   	             },
	   	             success : function(result) {
	   	            	layer.closeAll('loading');
	   	             	if (result.success) {
	   	             		layer.msg('新增成功!');
	   	             	    setTimeout('loadContent("/org/orgList");',500); 
	   	             	 
   	             		} else {
   	             			layer.msg(result.msg, {icon: 5});
	   	             	}
	   	             },  
                     error: function(){  
                    	 layer.closeAll('loading');
                    	 layer.msg('新增失败!', {icon: 5});  
                     } 
	   	     	});
	     		} 
		}; 
		
		//取消新增
		function hideAddOrg(){
			$("#orgDetail").show();
			$("#addOrg").hide();
		}
		//==========================新增树end===================================//
		
		
		
	</script>  
	<!-- 解决ie 6 7 8 9不能显示placeholder的问题 -->
	<script src="${ctx}/js/lib/jquery.JPlaceholder.js"></script>
